{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 以下三行是汉化过程中测试用代码，学习者在自己的环境中应删除或注释掉\n",
    "导入 sys\n",
    "导入 os\n",
    "sys.path.追加(os.路径.绝对路径(r'G:\\grasspy_zwmodules\\bs4\\zwbs\\src'))\n",
    "\n",
    "导入 网络请求\n",
    "从 蜘蛛网 导入 〇蜘蛛网\n",
    "\n",
    "响应 = 网络请求.查_get(\"https://www.baidu.com\")\n",
    "蜘蛛网 = 〇蜘蛛网(响应.内容, 特性=\"html.parser\")  # 代表被解析的文档整体"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<b><input autofocus=\"\" class=\"bg s_btn\" id=\"su\" type=\"submit\" value=\"百度一下\"/></b>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 修改蜘蛛网（文档树）\n",
    "# 更改标签名称和属性\n",
    "标签 = 蜘蛛网.查找全部('span')[1]\n",
    "标签\n",
    "标签.名称 = 'b'\n",
    "标签['class'] = '特粗'\n",
    "标签['id'] = 1\n",
    "标签\n",
    "删 标签['class']\n",
    "删 标签['id']\n",
    "标签"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<a class=\"mnav\" href=\"http://news.baidu.com\" name=\"tj_trnews\">八卦</a>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 更改字符串\n",
    "蜘蛛网.a\n",
    "蜘蛛网.a.字符串 = '八卦'\n",
    "蜘蛛网.a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<a class=\"mnav\" href=\"http://news.baidu.com\" name=\"tj_trnews\">八卦绯闻</a>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "蜘蛛网.a.追加('绯闻')\n",
    "蜘蛛网.a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['八卦', '绯闻', ', ', '流言蜚语']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "蜘蛛网.a.扩充([', ', '流言蜚语'])\n",
    "蜘蛛网.a\n",
    "蜘蛛网.a.内容々"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<a class=\"mnav\" href=\"http://news.baidu.com\" name=\"tj_trnews\">八卦绯闻, 流言蜚语<!--呵呵--></a>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "从 蜘蛛网 导入 〇注释\n",
    "注释 = 〇注释('呵呵')\n",
    "蜘蛛网.a.追加(注释)\n",
    "蜘蛛网.a\n",
    "# 蜘蛛网.a.内容々"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<a class=\"mnav\" href=\"http://news.baidu.com\" name=\"tj_trnews\">八卦绯闻, 流言蜚语<!--呵呵--><b>嘿嘿</b></a>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "新标签 = 蜘蛛网.新标签('b')\n",
    "新标签.字符串 = '嘿嘿'\n",
    "蜘蛛网.a.追加(新标签)\n",
    "蜘蛛网.a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<a class=\"mnav\" href=\"http://news.baidu.com\" name=\"tj_trnews\">八卦哈哈绯闻, 流言蜚语<!--呵呵--><b>嘿嘿</b></a>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "蜘蛛网.a.插入(1, '哈哈')\n",
    "蜘蛛网.a\n",
    "# 蜘蛛网.a.内容々"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<p id=\"lh\"> <span>好123</span><a href=\"http://home.baidu.com\">关于百度</a> <a href=\"http://ir.baidu.com\">About Baidu</a> </p>"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "新标签2 = 蜘蛛网.新标签('span')\n",
    "新标签2.字符串 = '好123'\n",
    "蜘蛛网.p.a.前插(新标签2)\n",
    "蜘蛛网.p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<p id=\"lh\"> <span>好123</span><a href=\"http://home.baidu.com\">关于百度</a><i>好456</i> 789 <i>好456</i> <a href=\"http://ir.baidu.com\">About Baidu</a> </p>"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "新标签3 = 蜘蛛网.新标签('i')\n",
    "新标签3.字符串 = '好456'\n",
    "蜘蛛网.p.a.后插(新标签3, ' 789 ')\n",
    "蜘蛛网.p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<p id=\"lh\"></p>"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "蜘蛛网.p.清空()\n",
    "蜘蛛网.p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 拔除()：返回被拔除的标签或字符串\n",
    "a标签 = 蜘蛛网.a\n",
    "# a标签\n",
    "b标签 = 蜘蛛网.a.b.拔除()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<b>嘿嘿</b>"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b标签"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "b标签.父元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'嘿嘿'"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b标签.字符串.拔除()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<b></b>"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b标签"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<noscript>  </noscript>"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "代脚本 = 蜘蛛网.noscript\n",
    "代脚本a标签 = 代脚本.a\n",
    "代脚本a标签.销毁()\n",
    "代脚本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "代脚本.已销毁\n",
    "代脚本a标签.已销毁"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hao123'"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 替换为()：替换字符串或标签\n",
    "草蟒网站 = 蜘蛛网.新标签('a', href=\"https://www.grasspy.cn\")\n",
    "草蟒网站.字符串 = '关于草蟒'\n",
    "蜘蛛网.a.查找下一兄弟元素().字符串.替换为(草蟒网站)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<a class=\"mnav\" href=\"https://www.hao123.com\" name=\"tj_trhao123\"><a href=\"https://www.grasspy.cn\">关于草蟒</a></a>"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "蜘蛛网.a.查找下一兄弟元素()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<a href=\"https://www.grasspy.cn\">关于草蟒</a>"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "蜘蛛网.a.查找下一兄弟元素().a.替换为('hao123')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<a class=\"mnav\" href=\"https://www.hao123.com\" name=\"tj_trhao123\">hao123</a>"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "蜘蛛网.a.查找下一兄弟元素()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['八卦', '哈哈', '绯闻', ', ', '流言蜚语', '呵呵']"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 蜘蛛网.a.字符串\n",
    "蜘蛛网.a.内容々"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['八卦哈哈绯闻, 流言蜚语', '呵呵']"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "蜘蛛网.a.平整()\n",
    "蜘蛛网.a.内容々"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<p><b>我希望以粗体显示</b></p>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "小树 = 〇蜘蛛网(\"<p>我希望以粗体显示</p>\", 'html.parser')\n",
    "小树.p.字符串.包裹(小树.新标签(\"b\"))\n",
    "小树"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<div><p><b>我希望以粗体显示</b></p></div>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "小树.p.包裹(小树.新标签(\"div\"))\n",
    "小树"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<p></p>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "小树.p.解包()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<div><b>我希望以粗体显示</b></div>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "小树"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'<b>\\xe6\\x88\\x91\\xe5\\xb8\\x8c\\xe6\\x9c\\x9b\\xe4\\xbb\\xa5\\xe7\\xb2\\x97\\xe4\\xbd\\x93\\xe6\\x98\\xbe\\xe7\\xa4\\xba</b>'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "小树.b.编码()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.1 ('.venv': venv)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.1+"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "9f23957215ae7e6101af7478a3aa6d946ec85d2106801d4efdfa4b8f6a5e1ee8"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
